<template>
  <div>
    <h1>OAuth Approval</h1>
    <p>你正在授权 {{ clientName }} 访问以下资源?</p>
    <el-form ref="form" label-width="80px">
      <el-form-item label="下列权限">
        <el-checkbox-group @change="checkNodes" v-model="scopeData">
          <el-checkbox
            v-for="m in scopeList"
            :label="m"
            name="type"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="get_auth_Code">授予</el-button>
        <el-button @click="onCancelAuth">拒绝</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getAuthorizeCode,
  get_confirm_access,
  getAuthorizeCallBack
} from '@/api/login'
export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loading: false,
      redirect: undefined,
      clientName: '',
      scopeList: [],
      scopeData: [],
      oauthModel: {
        client_id: '',
        response_type: '',
        redirect_uri: '',
        state: '',
        scope: ''
      }
    }
  },
  created() {
    this.confirm_access()
  },
  methods: {
    checkNodes(arr) {
      this.oauthModel.scope = arr.join()
    },
    onCancelAuth() {
      console.log('取消授权')
    },
    confirm_access() {
      var data = {
        client_id: this.$route.query.client_id,
        response_type: this.$route.query.response_type,
        redirect_uri: this.$route.query.redirect_uri,
        state: this.$route.query.state
      }
      get_confirm_access(data).then(res => {
        this.clientName = res.clientName
        this.scopeList = res.scope
        this.oauthModel.client_id = res.client_id
        this.oauthModel.response_type = this.$route.query.response_type
        this.oauthModel.redirect_uri = this.$route.query.redirect_uri
        this.oauthModel.state = res.state
        this.scopeData = res.scope
      })
    },
    get_auth_Code() {
      var data = {
        client_id: this.oauthModel.client_id,
        response_type: this.oauthModel.response_type,
        redirect_uri: this.oauthModel.redirect_uri,
        scope: this.oauthModel.scope
      }
      window.location.href =
        'http://localhost/dev-api/oauth/authorize?client_id=appb&response_type=code&redirect_uri=https://www.baidu.com&scope=scope1'
      /*  getAuthorizeCode(data).then(res => {
        debugger
        //this.$router.push({ path: res.returnUrl })
        //getAuthorizeCallBack(res.returnUrl)
        //window.location.href = res.returnUrl
      }) */
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
</style>
